package com.hulu.yec.teach.testpaper.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.hulu.yec.common.exception.DaoRuntimeException;
import com.hulu.yec.common.util.DBUtil;
import com.hulu.yec.teach.testpaper.entity.TestPaperEntity;
/**
 * 查询试卷详情
 * @author MYY
 *
 */
public class PaperDetailDao {
     public TestPaperEntity detail(int id){
		TestPaperEntity testPaper = new TestPaperEntity();
		 StringBuilder sb= new StringBuilder();
		 sb.append("SELECT p.id, p.name, p.paper_url AS 'paperUrl', ");
		 sb.append("p.paper_type AS 'paperType', c.name AS 'courseName',p.total_point AS 'totalPoint', ");
		 sb.append("p.pass_point AS 'passPoint', ");
		 sb.append("p.total_minutes AS 'totalMinutes', ");
		 sb.append("p.single_option_number AS 'singleOptionNumber', ");
		 sb.append("p.single_option_each_point AS 'singleOptionEachPoint', ");
		 sb.append("p.single_option_difficulty AS 'singleOptionDifficulty', ");
		 sb.append("p.multi_option_number AS 'multiOptionNumber', ");
		 sb.append("p.multi_option_each_point AS 'multiOptionEachPoint', ");
		 sb.append("p.multi_option_difficulty AS 'multiOptionDifficulty', ");
		 sb.append("p.judge_number AS 'judgeNumber', ");
		 sb.append("p.judge_each_point AS 'judgeEachPoint', ");
		 sb.append("p.judge_difficulty AS 'judgeDifficulty', ");
		 sb.append("u.name AS 'userName', ");
		 sb.append("p.opreate_time AS 'opreateTime', ");
		 sb.append("p.state,p.memo ");
		 sb.append("FROM  qes_exam_paper p,sys_course c,sys_user u ");
		 sb.append("WHERE p.course_id = c.id ");
		 sb.append("AND u.id=p.opreate_user_id ");
		 sb.append("AND p.id = ?");
		 
    	 Connection connection = null;
    	 PreparedStatement preparedStatement = null;
    	 ResultSet resultSet = null;
    	 try {
 			connection = DBUtil.getConnection();
 			preparedStatement = connection.prepareStatement(sb.toString());
 			preparedStatement.setInt(1, id);
 			resultSet = preparedStatement.executeQuery();
 			while (resultSet.next()) {
 				testPaper.setId(resultSet.getInt("id"));
 				testPaper.setName(resultSet.getString("name"));
 				testPaper.setPaperUrl(resultSet.getString("paperUrl"));
 				testPaper.setPaperType(resultSet.getInt("paperType"));
 				testPaper.setCourseName(resultSet.getString("courseName"));
 				testPaper.setTotalPoint(resultSet.getInt("totalPoint"));
 				testPaper.setPassPoint(resultSet.getInt("passPoint"));
 				testPaper.setTotalMinutes(resultSet.getInt("totalMinutes"));
 				testPaper.setSingleOptionNumber(resultSet.getInt("singleOptionNumber"));
 				testPaper.setSingleOptionEachPoint(resultSet.getInt("singleOptionEachPoint"));
 				testPaper.setSingleOptionDifficulty(resultSet.getInt("singleOptionDifficulty"));
 				testPaper.setMultiOptionNumber(resultSet.getInt("multiOptionNumber"));
 				testPaper.setMultiOptionEachPoint(resultSet.getInt("multiOptionEachPoint"));
 				testPaper.setMultiOptionDifficulty(resultSet.getInt("multiOptionDifficulty"));
 				testPaper.setJudgeNumber(resultSet.getInt("judgeNumber"));
 				testPaper.setJudgeEachPoint(resultSet.getInt("judgeEachPoint"));
 				testPaper.setJudgeDifficulty(resultSet.getInt("judgeDifficulty"));
 				testPaper.setUserName(resultSet.getString("userName"));
 				testPaper.setOpreateTime(resultSet.getDate("opreateTime"));
 				testPaper.setState(resultSet.getInt("state"));
 				testPaper.setMemo(resultSet.getString("memo"));
 			}
 		} catch (SQLException e) {
 			e.printStackTrace();
 			throw new DaoRuntimeException("sql错误");
 		} finally {
 			DBUtil.close(connection, preparedStatement, resultSet);
 		}
    	    return testPaper;
    }
 
}